<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
      <!--搜索条件-->
      <el-row>
        <el-col :span="7">
          <el-form-item label="店铺" prop="storeId">
            <el-select v-model="queryParams.storeId" filterable placeholder="请选择店铺">
              <el-option v-for="item in storeMgmtList" :key="item.id" :label="item.storeAbbr" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="商品标签" prop="goodsLabel">
            <el-input placeholder="请输入商品标签" v-model="queryParams.goodsLabel"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="统计日期" prop="statDate">
            <el-date-picker
              v-model="queryParams.statDateRange"
              @change="statDateChange"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item label="上新日期" prop="upNewDate">
            <el-date-picker
              v-model="queryParams.upNewDateRange"
              @change="upNewDateChange"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="商品访客数" prop="goodsVisitorNumber">
            <el-input v-model="queryParams.goodsVisitorNumberMin" placeholder="请输入" style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.goodsVisitorNumberMax" placeholder="请输入" style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="收藏加购率" prop="collectTreasureBuyRate">
            <el-input v-model="queryParams.collectTreasureBuyRateMin" placeholder="请输入" style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.collectTreasureBuyRateMax" placeholder="请输入" style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="访客平均价值" prop="visitorAverageValue">
            <el-input v-model="queryParams.visitorAverageValueMin" placeholder="请输入" style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.visitorAverageValueMax" placeholder="请输入" style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="推广占比" prop="promotionRatio">
            <el-input v-model="queryParams.promotionRatioMin" placeholder="请输入" style="width: 150px;">
              <template #suffix>%</template>
            </el-input>
            <span> - </span>
            <el-input v-model="queryParams.promotionRatioMax" placeholder="请输入" style="width: 150px;">
              <template #suffix>%</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付金额" prop="payAmount">
            <el-input v-model="queryParams.payAmountMin" placeholder="请输入" style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.payAmountMax" placeholder="请输入" style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支付买家数" prop="payBuyerNumber">
            <el-input v-model="queryParams.payBuyerNumberMin" placeholder="请输入" style="width: 150px;"></el-input>
            <span> - </span>
            <el-input v-model="queryParams.payBuyerNumberMax" placeholder="请输入" style="width: 150px;"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" icon="el-icon-set-up" @click="handleSet">指标标准</el-button>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="resetQuery"></right-toolbar>
    </el-row>

    <div ref="divTable">
      <el-table :height="showSearch?'46vh':'68vh'" v-loading="loading" :key="tableTime" :data="unsalableManageList" @sort-change='sortChange'>
        <el-table-column label="商品ID"  align="center" prop="goodsId" />
        <el-table-column label="商品图片"  align="center" prop="imgUrl">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imgUrl"
              :preview-src-list="[scope.row.imgUrl]"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="上新日期"  align="center" prop="upNewDate" sortable='custom'/>
        <el-table-column :label=columnLabel.goodsVisitorNumber  align="center" prop="goodsVisitorNumber" sortable='custom'>
          <template slot-scope="scope">
            <span v-if="scope.row.goodsVisitorNumberLevel===null">
              {{scope.row.goodsVisitorNumber}}
            </span>
            <span v-else>
              {{scope.row.goodsVisitorNumberLevel}} {{scope.row.goodsVisitorNumber}}
            </span>
          </template>
        </el-table-column>
        <el-table-column :label=columnLabel.collectTreasureBuyRate  align="center" prop="collectTreasureBuyRate" sortable='custom'>
          <template slot-scope="scope">
            <span v-if="scope.row.collectTreasureBuyRateLevel===null">
              {{scope.row.collectTreasureBuyRate}}
            </span>
            <span v-else>
              {{scope.row.collectTreasureBuyRateLevel}} {{scope.row.collectTreasureBuyRate}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="支付金额" align="center" prop="payAmount" sortable='custom'/>
        <el-table-column label="搜索访客数" align="center" prop="searchVisitorNumber" sortable='custom'/>
        <el-table-column label="推广花费" align="center" prop="promotionExpenses"/>
        <el-table-column label="商品数据判定类型" align="center" prop="judgeType" width="130">
          <template slot-scope="scope" v-if="scope.row.judgeType">
            <el-tag>{{scope.row.judgeType}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="行动点" align="center" prop="actionPoint" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <span style="color: #67C23A" v-if="scope.row.backLogCount!==null&&scope.row.backLogCount>0">
              近30天内已生成
            </span>
            <el-button
              :disabled="scope.row.isGenerateFlag"
              type="text"
              icon="el-icon-circle-plus"
              @click="handleGenerate(scope.row, scope.$index)"
            >生成待办事项</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :pageRefs="this.$refs"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-dialog title="设置指标标准" :visible.sync="open" width="500px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="auto">
        <el-form-item label="商品访客数" prop="goodsVisitorNumber">
          <el-input placeholder="请输入商品访客数" type="number" v-model="queryParams.goodsVisitorNumber"></el-input>
        </el-form-item>
        <el-form-item label="收藏加购率" prop="collectTreasureBuyRate">
          <el-input placeholder="请输入收藏加购率" type="number" v-model="queryParams.collectTreasureBuyRate"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSet">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getNormalStoreMgmtList} from "@/api/business/storeMgmt";
import {listUnsalableManage} from "@/api/business/unsalableBoard";
import {addUnsalableBacklog} from "@/api/business/unsalableBacklog";

export default {
  name: "UnsalableBoard",
  data(){
    return{
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      //链接管理看板集合数据
      unsalableManageList: [],
      //店铺集合
      storeMgmtList: [],
      open:false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId:null,
        goodsLabel:null,
        statDateRange:null,
        statDateStart:null,
        statDateEnd:null,
        upNewDateRange:null,
        upNewDateStart:null,
        upNewDateEnd:null,
        goodsVisitorNumberMin:null,
        goodsVisitorNumberMax:null,
        collectTreasureBuyRateMin:null,
        collectTreasureBuyRateMax:null,
        visitorAverageValueMin:null,
        visitorAverageValueMax:null,
        promotionRatioMin:null,
        promotionRatioMax:null,
        payAmountMin:null,
        payAmountMax:null,
        payBuyerNumberMin:null,
        payBuyerNumberMax:null,
        // 标准
        goodsVisitorNumber:null,
        collectTreasureBuyRate:null
      },
      columnLabel:  {
        goodsVisitorNumber: '商品访客数',
        collectTreasureBuyRate: '收藏加购率'
      },
      // 刷新表格
      tableTime: 0,
    }
  },
  created() {
    this.getNormalStoreMgmtList()
  },
  methods: {
    //后端全页面动态排序
    sortChange(column){
      this.queryParams.order=null
      this.queryParams.prop=null
      if (column.order==="ascending"){
        this.queryParams.order="asc"
      }else if (column.order==="descending"){
        this.queryParams.order="desc"
      }

      this.queryParams.prop=column.prop;

      this.getList()
    },
    getList() {
      this.loading = true;
      listUnsalableManage(this.queryParams).then(response => {
        this.unsalableManageList = response.rows;
        this.unsalableManageList.forEach(item=>{
          this.$set(item, 'isGenerateFlag', item.judgeType === null || item.actionPoint === null);
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    //指标设置弹窗
    handleSet(){
      this.open = true;
    },
    //确认设置
    confirmSet(){
      this.columnLabel =  {
        goodsVisitorNumber: this.queryParams.goodsVisitorNumber===null?'商品访客数':'商品访客数  '+this.queryParams.goodsVisitorNumber,
        collectTreasureBuyRate: this.queryParams.collectTreasureBuyRate===null?'收藏加购率':'收藏加购率  '+this.queryParams.collectTreasureBuyRate
      }
      this.tableTime = new Date().getTime()
      this.open=false
    },
    //取消设置
    cancel(){
      this.queryParams.goodsVisitorNumber=null
      this.queryParams.collectTreasureBuyRate=null
      this.open=false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$resetForm("queryForm");
      this.queryParams={
        pageNum: 1,
        pageSize: 10,
        storeId:null,
        goodsLabel:null,
        statDateRange:null,
        statDateStart:null,
        statDateEnd:null,
        upNewDateRange:null,
        upNewDateStart:null,
        upNewDateEnd:null,
        goodsVisitorNumberMin:null,
        goodsVisitorNumberMax:null,
        collectTreasureBuyRateMin:null,
        collectTreasureBuyRateMax:null,
        visitorAverageValueMin:null,
        visitorAverageValueMax:null,
        promotionRatioMin:null,
        promotionRatioMax:null,
        payAmountMin:null,
        payAmountMax:null,
        payBuyerNumberMin:null,
        payBuyerNumberMax:null,
        // 标准
        goodsVisitorNumber:null,
        collectTreasureBuyRate:null
      }

      //重置表头
      this.columnLabel= {
        goodsVisitorNumber: '商品访客数',
        collectTreasureBuyRate: '收藏加购率'
      }

      //重置表格内容
      this.total=0
      this.unsalableManageList= []
    },
    //查询店铺列表
    getNormalStoreMgmtList(){
      getNormalStoreMgmtList().then(res => {
        this.storeMgmtList = res.data
      })
    },
    //统计日期组件改变值
    statDateChange(){
      this.queryParams.statDateStart=this.queryParams.statDateRange[0]
      this.queryParams.statDateEnd=this.queryParams.statDateRange[1]
    },
    //上新日期组件改变值
    upNewDateChange(){
      this.queryParams.upNewDateStart=this.queryParams.upNewDateRange[0]
      this.queryParams.upNewDateEnd=this.queryParams.upNewDateRange[1]
    },
    //生成待办事项
    handleGenerate(row, index) {
      const data={...row,goodsVisitorNumberStandard:this.queryParams.goodsVisitorNumber,collectTreasureBuyRateStandard:this.queryParams.collectTreasureBuyRate}
      addUnsalableBacklog(data).then(response => {
        this.$set(row, 'isGenerateFlag', true)
        this.$modal.msgSuccess("待办事项生成成功");
      })
    }
  }
}
</script>
